<template>
  <div>
    <hr />
    <h1>watch监听器 侦听器</h1>
    <input type="text" v-model="firstName" />
    <input type="text" v-model="lastName" />'
    <button @click="changeObj">更改obj的值</button>
    <button @click="get$watch">查看$watch</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      firstName: "a",
      lastName: "b",
      obj: {
        name: "lele",
        age: 18,
      },
    };
  },
  methods: {
    changeObj() {
      this.obj.age += 1;
    },
    get$watch() {
      //   console.log(this.$watch);
      this.$watch(
        "lastName",
        (newValue, oldValue) => {
          console.log(newValue, oldValue);
        },
        {
          immediate: true,
        }
      );
    },
  },
  watch: {
    // 当监听的值发生改变 就会触发对应的回调函数
    // 页面展示时就要触发firstName对应的回调函数  立即触发
    /*  firstName(value) {
      console.log(value);
    }, */
    firstName: {
      /* handler:function(value) {
        console.log(value);
      }, */
      handler(value) {
        console.log(value);
      },
      immediate: true,
    },
    obj: {
      handler(value) {
        console.log(value);
      },
      deep: true,
    },
  },
};
</script>

<style lang="less" scoped>
</style>